package com.inossem.warehousestorage_inossem.utils;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.Pullable;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.exception.InossemException;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.borrow.Borrow_ManageActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.ChangeStorageManageActivity;
import com.inossem.warehousestorage_inossem.activity.down_frame.DownFrame_ManageActivity;
import com.inossem.warehousestorage_inossem.activity.exception.Exception_QueryActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageManageActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.Inventory_ManageActivity;
import com.inossem.warehousestorage_inossem.activity.my.My_BillActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageManageActivity;
import com.inossem.warehousestorage_inossem.activity.picking.Picking_ManageActivity;
import com.inossem.warehousestorage_inossem.activity.query.QueryManageActivity;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveManageActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageManageActivity;
import com.inossem.warehousestorage_inossem.activity.storage.Storage_ManageActivity;
import com.inossem.warehousestorage_inossem.activity.system.SystemSettingActivity;
import com.inossem.warehousestorage_inossem.activity.transport.Transport_ManageActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.UpFrame_ManageActivity;
import com.inossem.warehousestorage_inossem.constant.Constant;
import com.inossem.warehousestorage_inossem.model.FunctionItem;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/6/7.
 */

public class ActivityUtils {

    public static final int REQUEST_CODE_SYSTEM_SETTING = 1001;

    private static TimePickerDialog.Builder yearMonthDay;

    public static void checkPullUp(Context context, Pullable pullable, int size) {
        if (pullable == null || size < 0) {
            throw new InossemException("parameter error");
        }
        if (size == 0) {
            pullable.setPullUpFlag(false);
            Utils.middleToast(context, context.getResources().getString(R.string.common_no_more_data));
        }
    }

    public static List getShowData(List list, int begin) {
        int end = begin + Constant.PAGE_SIZE;
        if (list.size() < end) {
            end = list.size();
        }
        return list.subList(begin, end);
    }

    public static PercentRelativeLayout initBackAndFunction(Activity activity, int resourceId, View.OnClickListener onclickListener) {
        return initBackAndFunction(activity, activity.getResources().getString(resourceId), onclickListener);
    }

    public static PercentRelativeLayout initBackAndFunction(Activity activity, String text, View.OnClickListener onclickListener) {
        PercentRelativeLayout leftTitleLayout = (PercentRelativeLayout) activity.findViewById(R.id.leftTitleLayout);
        View backAndNameView = LayoutInflater.from(activity).inflate(R.layout.include_back_and_name, null);
        TextView functionName = (TextView) backAndNameView.findViewById(R.id.functionName);
        leftTitleLayout.addView(backAndNameView);
        functionName.setText(text);
        leftTitleLayout.setOnClickListener(onclickListener);
        return leftTitleLayout;
    }

    public static PercentRelativeLayout initRightFunction(Activity activity, View view, View.OnClickListener onclickListener) {
        PercentRelativeLayout rightTitleLayout = (PercentRelativeLayout) activity.findViewById(R.id.rightTitleLayout);
        rightTitleLayout.addView(view);
        rightTitleLayout.setOnClickListener(onclickListener);
        return rightTitleLayout;
    }

    public static List<FunctionItem> getFunctionItemList(Context context) {
        List<FunctionItem> result = new ArrayList<>();
        FunctionItem functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_receive_manage);
        functionItem.setId("20101");
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_receive_manage_title));
        functionItem.setClassName(ReceiveManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_in_storage_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_in_storage_manage_title));
        functionItem.setId("202");
        functionItem.setClassName(InStorageManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_picking_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_picking_manage_title));
        functionItem.setId("204");
        functionItem.setClassName(Picking_ManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_out_storage_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_out_storage_manage_title));
        functionItem.setId("206");
        functionItem.setClassName(OutStorageManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_return_storage_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_return_storage_manage_title));
        functionItem.setId("207");
        functionItem.setClassName(RefundStorageManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_change_storage_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_change_storage_manage_title));
        functionItem.setId("21001");
        functionItem.setClassName(ChangeStorageManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_test);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_transport_manage_title));
        functionItem.setClassName(Transport_ManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_test);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_borrow_manage_title));
        functionItem.setClassName(Borrow_ManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_up_frame_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_up_frame_manage_title));
        functionItem.setId("20301");
        functionItem.setClassName(UpFrame_ManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_test);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_down_frame_manage_title));
        functionItem.setClassName(DownFrame_ManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_storage_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_storage_manage_title));
        functionItem.setId("20302");
        functionItem.setClassName(Storage_ManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_inventory_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_inventory_manage_title));
        functionItem.setId("209");
        functionItem.setClassName(Inventory_ManageActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_test);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_exception_query_title));
        functionItem.setClassName(Exception_QueryActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_test);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_my_bill_title));
        functionItem.setClassName(My_BillActivity.class);
        result.add(functionItem);

        functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_query_manage);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_query_manage_title));
        functionItem.setId("3");
        functionItem.setClassName(QueryManageActivity.class);
        result.add(functionItem);

        return result;
    }

    public static List<FunctionItem> getCommonFunctionItemList(Context context) {
        List<FunctionItem> result = new ArrayList<>();
        FunctionItem functionItem = new FunctionItem();
        functionItem.setPic(R.drawable.function_logo_system_setting);
        functionItem.setFunctionName(context.getResources().getString(R.string.activity_system_setting_title));
        functionItem.setForResult(true);
        functionItem.setClassName(SystemSettingActivity.class);
        result.add(functionItem);

        return result;
    }

    public static TimePickerDialog getTimePickerDialog(Context context, String hint, String time, OnDateSetListener onDateSetListener) {
        TimePickerDialog.Builder yearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setThemeColor(ContextCompat.getColor(context, R.color.include_header_background))
                .setCallBack(onDateSetListener)
                .setTitleStringId(context.getResources().getString(R.string.common_select_date));
        if (!hint.equals(time)) {
            yearMonthDay.setCurrentMillseconds(Utils.getTimeStampFromString(UtilsConstant.YYYY_MM_DD, time));
        }
        return yearMonthDay.build();
    }

}
